/* 函数组件,状态,状态之间相互影响 */
import React, { useReducer } from 'react'

export default function Reducer03() {
    // const [count, dispatch] = useReducer((state, action) => {
    const [count, dispatch] = useReducer((state, action) => {
        const { type, num} = action
        console.log("当前的值",state)
        console.log('dispatch触发的条件',action)
        switch (type) {
            case "add": 
                // 返回的是新值
                return state + num;
            case "sup":
                return state - num;
            default:
                return state;
        }
    }, 0)
    // 默认值是0
    return (
        <div>
            {count}
            <br />
            <button onClick={() => {dispatch({type:'add',num:2}) }}>相加2</button>
            <button onClick={() => {dispatch({type:'sup',num:2}) }}>相减2</button>
            <button onClick={() => {dispatch({type:'*'}) }}>保持不变</button>
        </div>
    )
}
